var common = require('../../../utils/common.js');
var app = getApp();
var baseUrl = app.url;

Page({
    data: {
        inputShowed: false,
        inputVal: "",
        files: [],
        datalist:{},
        descNum:0,
        roleIndex:0,  
        agency_id:0,
        count:0, 
    },
    onLoad:function (options){
        var id = options.id;
        this.setData({id:id})
        common.bestCurl('GET','/admin/edit',res=>{
            if(res.statusCode == 200){
                this.setData({agency:res.data.agency,roles:res.data.roles,details:res.data.admin})
            }
        },{'id':id})
    },
    bindRoleChange: function(e) {
        this.setData({
            roleIndex: e.detail.value
        })
    },
    bindAgencyChange: function(e) {
        this.setData({
            agency_id: e.detail.value
        })
    },
    descChange:function (event) {
        this.setData({descNum:event.detail.cursor});   
    },
    numberChange:function(e){
        var user_name = e.detail.value;
        if (user_name.length == 0 ){
            wx.showToast({
                title: '手机号码不得为空',
                icon: 'none',
                duration:1500
            })
        }else if(user_name.length!=11){
            wx.showToast({
                title: '请输入11位手机号码',
                icon: 'none',
                duration:2000
            })
        }else if(isNaN(user_name)){
            wx.showToast({
                title: '请输入数字',
                icon: 'none',
                duration:2000
            }) 
        }
    },
    checkEmail:function(e){
        var email = e.detail.value;
        var extStart = email.indexOf('@');
        var ext = email.lastIndexOf('.');
        if(extStart<1 || ext<extStart+2 || ext+2>=email.length){
            wx.showToast({
                title: '邮箱格式不正确',
                icon: 'none',
                duration: 2000
            }); 
        }
    },
    validate:function(formData){ 
        var count = this.data.count;
        if (formData.user_name.length == 0 ){
            wx.showToast({
                title: '手机号码不得为空',
                icon: 'none',
                duration:2000
            })
            return false;
        }else if(formData.user_name.length!=11){
            wx.showToast({
                title: '请输入11位手机号码',
                icon: 'none',
                duration:2000
            })
            return false;
        }else if (isNaN(formData.user_name)){
            wx.showToast({
                title: '请输入数字',
                icon: 'none',
                duration:2000
            })
            return false;
        }else if(formData.nick_name==''){
            wx.showToast({
                title: '昵称不能为空',
                icon: 'none',
                duration:2000
            })
            return false;
        }else if(formData.email.indexOf('@')<1 || formData.email.lastIndexOf('.')<formData.email.indexOf('@')+2
            || formData.email.lastIndexOf('.')+2>=formData.email.length)
        {
            wx.showToast({
                title: '邮箱格式不正确',
                icon: 'none',
                duration: 2000
            })
            return false; 
        }
        this.setData({count:1})
    },
    formSubmit:function (event) {
        var id = this.data.id;
        var formData = event.detail.value;
        this.validate(formData);
        var count = this.data.count;
        console.log(count)
        if(count == 1){
            var appData = this.data;
            var data = [];
            data['agency_id'] = appData.agency[appData.agency_id].id;
            data['role_id'] = appData.roles[appData.roleIndex].id;
            common.bestCurl('POST','/admin/edit',res=>{
                if(res.statusCode == 200){
                    if(res.data.code == 1){
                        wx.showToast({
                            title: '更新成功',
                            icon: '',
                            duration: 5000
                        });
                        setTimeout(res=>{
                            wx.redirectTo({
                                url: '../list/list'
                            });
                        },2000);
                    }else if(res.data.code == 2){
                        wx.showToast({
                            title: '更新失败,请重试',
                            icon: 'none',
                            duration: 5000
                        });
                    }
                }
            },Object.assign({'data':data},{'id':id},formData))
        }
    }
});

